package com.humble.controller;/*
  humble keep gonging
*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.humble.bean.Message;
import com.humble.bean.furn;
import com.humble.dao.furnMapper;
import com.humble.service.FurnService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Controller
public class TestController {
    @Resource
    private FurnService furnService;


    @RequestMapping(value = "/hi")
    public String hi() {
        return "hi";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Message save(@Validated @RequestBody furn furn, Errors errors) {
        if (errors.hasErrors()) {
            List<FieldError> fieldErrors = errors.getFieldErrors();
            HashMap<String, String> errorsMap = new HashMap<>();
            for (FieldError fieldError : fieldErrors) {
                errorsMap.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            Message fail = Message.fail();
            fail.addMes("errors",errorsMap);
            return fail;
        }
        try {
            furnService.save(furn);
            return Message.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Message.fail();
        }
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Message list() {
        try {
            List<furn> allFurns = furnService.getAllFurns();
            Message success = Message.success();
            success.addMes("list", allFurns);
            return success;
        } catch (Exception e) {
            return Message.fail();
        }
    }

    @PutMapping(value = "/update")
    @ResponseBody
    public Message update(@RequestBody furn furn) {
        try {
            furnService.update(furn);
        } catch (Exception e) {
            return Message.fail();
        }
        return Message.success();
    }

    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public Message delete(@PathVariable Integer id) {
        try {
            System.out.println("删除请求：" + id);
            furnService.delete(id);
        } catch (Exception e) {
            return Message.fail();
        }
        return Message.success();
    }

    @PostMapping(value = "/get/{id}")
    @ResponseBody
    public Message get(@PathVariable Integer id) {
        System.out.println("收到请求");
        System.out.println(id);
        furn furnById = null;
        Message success = null;
        try {
            furnById = furnService.getFurnById(id);
            success = Message.success();
            success.addMes("furn", furnById);
        } catch (Exception e) {
            return Message.fail();
        }

        return success;
    }

    @RequestMapping(value = "/getFurnsByPage")
    @ResponseBody
    public Message getfurnsByPage(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "5") Integer size) {
        //设置分页拦截器
        PageHelper.startPage(page, size);
        //用查询全部的方法，但拦截器会在发出sql语句前进行修改；
        // 分页使用limit关键字进行限制
        List<furn> furns = furnService.getAllFurns();
        //将查询到的信息以及每页数量封装进对象中；
        // 该对象还含有表记录总数等信息，方便前端进行获取；
        PageInfo<furn> pageInfo = new PageInfo<>(furns, size);
        Message success = Message.success();
        success.addMes("page", pageInfo);
        return success;
    }

    @RequestMapping(value = "/getByName")
    @ResponseBody
    public Message getByName(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             String name) {
        try {
            PageHelper.startPage(page, pageSize);
            List<furn> furnByName = furnService.getFurnByName(name);
            PageInfo<furn> pageInfo = new PageInfo<>(furnByName,pageSize);
            Message success = Message.success();
            success.addMes("page", pageInfo);
            return success;
        } catch (Exception e) {
            return Message.fail();
        }
    }
}
